//
//  TYZImagePlayView.m
//  ESTSCRMAPP
//
//  Created by 李贤立 on 2018/10/17.
//  Copyright © 2018年 Capfer. All rights reserved.
//

#import "TYZImagePlayView.h"
#import <SDCycleScrollView/SDCycleScrollView.h>
#import "UIImageView+WebCache.h"

@interface TYZImagePlayView ()<SDCycleScrollViewDelegate>
/**
 *  imagePlayView
 */
@property(nonatomic, strong) SDCycleScrollView *imagePlayView;
@end

@implementation TYZImagePlayView
#pragma mark - 创建方法
- (instancetype)init {
    if (self = [super init]) {
        self.userInteractionEnabled = YES;
        // 单击退出手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
        tap.numberOfTapsRequired = 1;
        [self addGestureRecognizer:tap];
        // 拖动图片手势
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panImageView:)];
        [self addGestureRecognizer:pan];
        // 长按保存手势
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressImageView:)];
        [self addGestureRecognizer:longPress];
        // 捏合缩放手势
        UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchImageView:)];
        [self addGestureRecognizer:pinch];
        // 旋转图片手势
        UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationImageView:)];
        [self addGestureRecognizer:rotation];
        // 轻扫翻页手势
        UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipImageView:)];
        [self addGestureRecognizer:swipe];
    }
    return self;
}
- (instancetype)initWithImageURLStringsGroup:(NSArray<NSString *> *)imageURLStringsGroup {
    if (self = [super init]) {
        [self setupCycleViewWithImageURLStringsGroup:(NSArray *)imageURLStringsGroup];
    }
    return self;
}
+ (instancetype)imagePlayInFullScreenWithImageURLStringsGroup:(NSArray<NSString *> *)imageURLStringsGroup {
    TYZImagePlayView *playView = [[TYZImagePlayView alloc] init];
    UIWindow *window = [TYZApplication sharedApplication].keyWindow;
    [window addSubview:playView];
    [playView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(window);
    }];
    SDCycleScrollView *imagePlayView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero delegate:playView placeholderImage:nil];
    [playView addSubview:imagePlayView];
    [imagePlayView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(playView);
    }];
    imagePlayView.backgroundColor = [UIColor blackColor];
    imagePlayView.imageURLStringsGroup = imageURLStringsGroup;
    imagePlayView.bannerImageViewContentMode = UIViewContentModeScaleAspectFit;
    playView.layer.masksToBounds = YES;
    __weak typeof(playView) weakPlayView = playView;
    [playView setDidClickCycleImageBlock:^(NSInteger index) {
        [weakPlayView removeFromSuperview];
    }];
    return playView;
}
- (void)setupCycleViewWithImageURLStringsGroup:(NSArray *)imageURLStringsGroup {
    SDCycleScrollView *imagePlayView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero delegate:self placeholderImage:[UIImage imageNamed:Image_PlaceholderImage_Normal]];
    [self addSubview:imagePlayView];
    [imagePlayView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    imagePlayView.imageURLStringsGroup = imageURLStringsGroup;
    imagePlayView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
    self.layer.masksToBounds = YES;
}
- (void)setImageURLStringsGroup:(NSArray<NSString *> *)imageURLStringsGroup {
    _imageURLStringsGroup = imageURLStringsGroup;
    [self setupCycleViewWithImageURLStringsGroup:imageURLStringsGroup];
}
#pragma mark - SDCycleScrollViewDelegate
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index {

}
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
    if (self.didClickCycleImageBlock) {
        self.didClickCycleImageBlock(index);
    }
}
#pragma mark - 图片的操作
#pragma mark - actions
- (void)tapImageView:(UITapGestureRecognizer *)recognizer {
    [self removeFromSuperview];
}
- (void)panImageView:(UIPanGestureRecognizer *)recognizer {
    //视图前置操作
    [recognizer.view.superview bringSubviewToFront:recognizer.view];
    CGPoint center = recognizer.view.center;
    CGFloat cornerRadius = recognizer.view.frame.size.width / 2;
    CGPoint translation = [recognizer translationInView:self];
    NSLog(@"%@", NSStringFromCGPoint(translation));
    recognizer.view.center = CGPointMake(center.x + translation.x, center.y + translation.y);
    [recognizer setTranslation:CGPointZero inView:self];
    
    if (recognizer.state == UIGestureRecognizerStateBegan) {
        
    }
    if (recognizer.state == UIGestureRecognizerStateEnded) {
        //计算速度向量的长度，当他小于200时，滑行会很短
        CGPoint velocity = [recognizer velocityInView:self];
        CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
        CGFloat slideMult = magnitude / 200;
        //NSLog(@"magnitude: %f, slideMult: %f", magnitude, slideMult); //e.g. 397.973175, slideMult: 1.989866
        
        //基于速度和速度因素计算一个终点
        float slideFactor = 0.1 * slideMult;
        CGPoint finalPoint = CGPointMake(center.x + (velocity.x * slideFactor),
                                         center.y + (velocity.y * slideFactor));
        //限制最小［cornerRadius］和最大边界值［self.view.bounds.size.width - cornerRadius］，以免拖动出屏幕界限
        finalPoint.x = MIN(MAX(finalPoint.x, cornerRadius),
                           self.bounds.size.width - cornerRadius);
        finalPoint.y = MIN(MAX(finalPoint.y, cornerRadius),
                           self.bounds.size.height - cornerRadius);
        
        //使用 UIView 动画使 view 滑行到终点
        [UIView animateWithDuration:slideFactor*2
                              delay:0
                            options:UIViewAnimationOptionCurveEaseOut
                         animations:^{
                             //                             recognizer.view.center = finalPoint;
                         }
                         completion:nil];
    }
}
- (void)longPressImageView:(UILongPressGestureRecognizer *)recognizer {
    
}
- (void)pinchImageView:(UIPinchGestureRecognizer *)recognizer {
    CGFloat scale = recognizer.scale;
    // 在已缩放大小基础下进行累加变化；区别于：使用CGAffineTransformMakeScale 方法就是在原大小基础下进行变化
    recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, scale, scale);
    recognizer.scale = 1.0;
}
- (void)rotationImageView:(UIRotationGestureRecognizer *)recognizer {
    recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
    recognizer.rotation = 0.0;
}
- (void)swipImageView:(UISwipeGestureRecognizer *)recognizer {
    // 根据轻扫方向，进行不同控制
    switch (recognizer.direction) {
        case UISwipeGestureRecognizerDirectionRight: {
            
            break;
        }
        case UISwipeGestureRecognizerDirectionLeft: {
            
            break;
        }
        case UISwipeGestureRecognizerDirectionUp: {
            
            break;
        }
        case UISwipeGestureRecognizerDirectionDown: {
            
            break;
        }
    }
}
@end
